
package tpcar.ejb;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Defines a statefull bean. This beans provides methods
 * for accessing the card entities.<br>
 * By default, an interface is considered as a local interface if there is no
 * @Remote annotation.
 */
@Stateful(mappedName="adminbean")
public class AdminBean implements Admin {

    /**
     * Entity manager utilise par ce bean.
     */
    @PersistenceContext
    private EntityManager entityManager = null;

   
		// currently useless
		private boolean isLogin ;

		public boolean login ( String username , String password ) {
			if ( username.equals (" admin ") && password.equals (" admin ")) {
				this.isLogin = true ;
				return true ;
			}
			return false ;
		}
		
		public boolean logout () {
			this.isLogin = false ;
			return true ;
		}

  /**
  * add a new card.
  * @return true if the creation succeed.
  */  
     public boolean addCarteDeVisite(String nom, String prenom, String email, String phone) {
        try {
        	
        	CleComposite cle = new CleComposite(nom,prenom);
        	Personne person = entityManager.find(Personne.class, cle);
        	if(person == null)
        		return false;
        	CarteDeVisite cv = new CarteDeVisite(person, email, phone);
        	entityManager.persist(cv);
        	person.addCarteDeVisite(cv);
        	return true;
        }catch(Exception e){
      		return false;
      	}
    }

@Override
public boolean delete(long id) {
	
	CarteDeVisite cv = entityManager.find(CarteDeVisite.class, id);
	if(cv == null)
			return false;
	CleComposite cle = new CleComposite(cv.getNom(),cv.getPrenom());
	Personne person = entityManager.find(Personne.class, cle);
	if(person == null)
		return false;
	entityManager.remove(cv);
	person.removeCarteDeVisite(cv);
	return true;
}
     
     
     

}
